from django.contrib.auth.models import User
from django.db import models


# Create your models here.

class AssignClass(models.Model):
    """
    任务分类表
    """
    class_name = models.CharField(max_length=20, verbose_name='任务类别名称')
    color = models.CharField(max_length=7, verbose_name='颜色',
                             help_text='例如默认黑色为：#000000<span style="background:#000000; color:#000000; margin-left:7px;">#000000</span>',
                             default='#000000')
    need_score = models.FloatField(verbose_name="学生需要达到学分")

    def __str__(self):
        return self.class_name

    class Meta:
        verbose_name = '第二课堂活动分类'
        verbose_name_plural = verbose_name


class Assignment(models.Model):
    """
    任务表
    """
    belong_user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='发布人')
    belong_class = models.ForeignKey(to=AssignClass, on_delete=models.CASCADE, verbose_name='任务类别')
    name = models.CharField(max_length=30, verbose_name='名称')
    credit = models.FloatField(verbose_name='学分')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    activity_time = models.DateTimeField(verbose_name='活动时间')
    site = models.CharField(verbose_name='地点', max_length=50)
    require = models.TextField(verbose_name='要求')
    remark = models.TextField(verbose_name='备注')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '第二课堂活动'
        verbose_name_plural = verbose_name


class AssignPics(models.Model):
    """
    任务图片表
    """
    belong_assign = models.ForeignKey(to=Assignment, on_delete=models.CASCADE, verbose_name='所属任务')
    pictures = models.ImageField(verbose_name='图片', upload_to='app_server/assign_pics/')

    class Meta:
        verbose_name = '任务图片'
        verbose_name_plural = verbose_name


class UserDetail(models.Model):
    """
    用户详情
    """
    to_user = models.OneToOneField(to=User, on_delete=models.CASCADE, verbose_name='所属用户')
    has_credit = models.FloatField(verbose_name='已有学分', default=0.0)
    require_credit = models.FloatField(verbose_name='所需学分', default=20.0)

    def __str__(self):
        return self.to_user.username

    class Meta:
        verbose_name = '用户详情表'
        verbose_name_plural = verbose_name


class UserLogin(models.Model):
    to_user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="所属用户")
    time_stamp = models.CharField(max_length=100, verbose_name="时间戳", blank=True, null=True)
    method = models.CharField(max_length=20, verbose_name="登录方式", blank=True, null=True)
    session_id = models.CharField(max_length=100, verbose_name="登录信息", blank=True, null=True)

    def __str__(self):
        return self.to_user.username

    class Meta:
        verbose_name = '用户登录cookie表'
        verbose_name_plural = verbose_name


class UserScore(models.Model):
    """
    学生课程分类成绩表
    """
    to_user_detail = models.ForeignKey(to=UserDetail, on_delete=models.CASCADE, verbose_name='所属用户')
    to_assign_class = models.ForeignKey(to=AssignClass, on_delete=models.CASCADE, verbose_name='所属分类')
    has_credit = models.FloatField(verbose_name='已有学分', default=0.0)

    def __str__(self):
        return self.to_user_detail.to_user.username

    class Meta:
        verbose_name = '用户活动分类成绩'
        verbose_name_plural = verbose_name


class StudentSelect(models.Model):
    """
    学生选课表
    """
    STATUS_CODE = ((0, '进行中'), (1, '已完成'), (-1, '未完成'))
    belong_user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name='用户名')
    select_lessons = models.ForeignKey(to=Assignment, on_delete=models.CASCADE, verbose_name='选择课程')
    status = models.IntegerField(verbose_name='状态', choices=STATUS_CODE, default=0)

    def __str__(self):
        return str(self.belong_user.username)

    class Meta:
        verbose_name = '用户审核'
        verbose_name_plural = verbose_name
